import csv
from datetime import datetime

from matplotlib import pyplot as plt

file_name = 'chapter_16_sitka_weather_07-2014.csv'

with open(file_name) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    # 打印第一行
    # print(header_row)

    # 打印第一行,每一列及其索引
    # for index, column_header in enumerate(header_row, 1):
    #     print(index, column_header)

    # 获取每一天最高温度
    # highs = [], dates = []
    dates, highs = [], []
    for row in reader:
        high = int(row[1])
        highs.append(high)

        current_date = datetime.strptime(row[0], "%Y-%m-%d")
        dates.append(current_date)
        # print(highs)

# 根据数据绘制图形
fig = plt.figure(dpi=128, figsize=(12, 6))
plt.plot(dates, highs, c='red')

# 设置图形的格式
plt.title("Daily high temperatures, July 2014", fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
